//
//  AXHomeZjzdEvaluateVC.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/12/11.
//  Copyright © 2018 tzhg. All rights reserved.
//

#import "AXHomeZjzdEvaluateVC.h"
#import "AXHomeZjzdEvaluateCell.h"
#import "AXHomeZjzdEvaluateModel.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "UIViewController+HBD.h"

@interface AXHomeZjzdEvaluateVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic , strong) UITableView *tableView;
@property (nonatomic , strong) NSMutableArray<AXHomeZjzdEvaluateModel *> *modelsArray;
@property (nonatomic, assign) NSInteger pageIndex; //页数

@end

@implementation AXHomeZjzdEvaluateVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setupNavigationItems];
    [self creatView];
    [self setlayout];
 
    /** 导航条白色 */
    self.hbd_barTintColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 下拉刷新
    self.tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self requestList];
        
    }];
    
    
    // 上拉刷新
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [self requestList];
    }];
    
    LKEmptyViewConfig *config = [LKEmptyViewConfig setupEmptyViewConfigForTableView:self.tableView];
    config.message = @"暂无数据";
    
    [self.tableView.mj_header beginRefreshing];
    
}


- (void)setupNavigationItems {
    
    /** 导航条左边的按钮 */
    //返回按钮
    UIButton *kfBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [kfBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    kfBtn.frame = CGRectMake(0, 0, 60, 30);
    [kfBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:kfBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.textColor = [UIColor colorWithHex:0x29b566];
    titleLabel.text = @"评论列表";
    self.navigationItem.titleView = titleLabel;
    
}

#pragma mark - Action

- (void)backAction:(UIBarButtonItem *)item {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)creatView {
    
    _tableView = [[UITableView alloc]init];
    _tableView.backgroundColor = [UIColor whiteColor];
    [_tableView registerClass:[AXHomeZjzdEvaluateCell class] forCellReuseIdentifier:[AXHomeZjzdEvaluateCell reuseID]];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
}

- (void)setlayout {
    
    _tableView.sd_layout
    .leftSpaceToView(self.view, 0)
    .topSpaceToView(self.view, NAVIGATION_HEIGHT)
    .widthIs(SCREEN_WIDTH)
    .heightIs(SCREEN_HEIGHT-NAVIGATION_HEIGHT);
    
    
}

#pragma mark - requestNetwork

- (void)requestList {
    
    ZX_WEAK_SELF
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"fExpertId"] = self.fExpertId;
    params[@"pageNum"] = @(self.pageIndex);
    params[@"pageSize"] = @15;
    
    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_AgricultureCircleProfessorDiagnose, URL_AgricultureCircleProfessorDiagnose_selectExpertEvaluatePage) success:^(id respose){
        
        if ([respose[@"header"][@"statusCode"] integerValue] == 200) {
            
            weakSelf.modelsArray = [AXHomeZjzdEvaluateModel  mj_objectArrayWithKeyValuesArray:respose[@"body"][@"data"][@"list"]];
            [self.tableView reloadData];
            
        } else if ([respose[@"header"][@"statusCode"] integerValue] != 200) {
            
        }
        
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
    } failure:^(NSError *error) {
        
        if (self.pageIndex > 1) {
            self.pageIndex--;
        }
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        
    }];
 
    
}


#pragma mark - tableViewDelegate & tableViewDasource


/** cell的高度 */

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
   
    
    return [tableView cellHeightForIndexPath:indexPath model:self.modelsArray[indexPath.row] keyPath:@"model" cellClass:[AXHomeZjzdEvaluateCell class] contentViewWidth:SCREEN_WIDTH];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.modelsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    AXHomeZjzdEvaluateCell *cell = [tableView dequeueReusableCellWithIdentifier:[AXHomeZjzdEvaluateCell reuseID] forIndexPath:indexPath];
    cell.model = self.modelsArray[indexPath.row];
    
    return cell;
    
    
}

- (NSMutableArray<AXHomeZjzdEvaluateModel *> *)modelsArray {
    
    if (!_modelsArray) {
        
        _modelsArray = [NSMutableArray<AXHomeZjzdEvaluateModel *> array];
    }
    return _modelsArray;
    
}


@end
